package com.cloudcc.mobile.widget.listview;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.cloudcc.cloudframe.ui.viewgroup.loadmore.LoadMoreContainer;
import com.cloudcc.cloudframe.ui.viewgroup.loadmore.LoadMoreHandler;
import com.cloudcc.cloudframe.ui.viewgroup.loadmore.LoadMoreListViewContainer;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrClassicDefaultHeader;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrClassicFrameLayout;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrDefaultHandler;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrFrameLayout;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrHandler;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.header.DefaultMaterialHeader;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.header.MaterialHeader;
import com.cloudcc.cloudframe.util.DensityUtils;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.weight.EmptyLayout;

import static com.taobao.accs.ACCSManager.mContext;

public class CloudCCListView extends PtrClassicFrameLayout implements
		PtrHandler, LoadMoreHandler {
	EmptyLayout empty_loading;
	LoadMoreListViewContainer mLoadMoreLayout;
	ListView mListView;

	public CloudCCListView(Context context) {
		this(context, null);
	}

	public CloudCCListView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public CloudCCListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		LayoutInflater.from(getContext()).inflate(
				R.layout.widget_refresh_empty_listview, this);
		mLoadMoreLayout = (LoadMoreListViewContainer) findViewById(R.id.load_more_list_view_container);
		mListView = (ListView) findViewById(R.id.listview);
		
		initRefresh();
		initLoadMore();
	}
	public void isfirst(){
		mListView.setSelection(0);
	}

	/**
	 * 初始化刷新
	 */
	protected void initRefresh() {
		setLoadingMinTime(1000);
		setPtrHandler(this);
		PtrClassicDefaultHeader header = new PtrClassicDefaultHeader(getContext());
//		final MaterialHeader header = new DefaultMaterialHeader(getContext());
//		int[] colors = getResources().getIntArray(R.array.google_colors);
//		header.setColorSchemeColors(colors);
//		header.setLayoutParams(new PtrFrameLayout.LayoutParams(-1, -2));
//		header.setPadding(0, DensityUtils.dpToPixel(getContext(), 10), 0,
//				DensityUtils.dpToPixel(getContext(), 5));
//		header.setPtrFrameLayout(this);
		setHeaderView(header);
		addPtrUIHandler(header);
		setResistance(1.5f);
	}

	/**
	 * 初始化 加载更多
	 */
	protected void initLoadMore() {
		mLoadMoreLayout.setAutoLoadMore(true);
		mLoadMoreLayout.setOffsetSize(3);
		mLoadMoreLayout.useDefaultHeader();
		mLoadMoreLayout.setLoadMoreHandler(this);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		addDefaultEmptyView();
	}

	private void addDefaultEmptyView() {
		if (empty_loading != null) {
			return;
		}
		empty_loading = new EmptyLayout(getContext());
		empty_loading.setLayoutParams(new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		((ViewGroup) mListView.getParent()).addView(empty_loading);
		mListView.setEmptyView(empty_loading);
		hide();
	}

	/**
	 * 获得容器内的listview
	 * 
	 * @return
	 */
	public ListView getListView() {
		return mListView;
	}

	public LoadMoreListViewContainer getLoadMore() {
		return mLoadMoreLayout;
	}

	/**
	 * 自动开启刷新请求数据
	 */
	public void requestRefresh() {
		postDelayed(new Runnable() {
			@Override
			public void run() {
				autoRefresh(true);
			}
		}, 150);
	}

	@Override
	public boolean checkCanDoRefresh(PtrFrameLayout frame, View arg1,
			View header) {
		return PtrDefaultHandler.checkContentCanBePulledDown(frame, mListView,
				header);
	}

	@Override
	public void onRefreshBegin(PtrFrameLayout frame) {
		// 开始下拉刷新请求数据
		if (mOnRefreshOrLoadMoreListener != null) {
			mOnRefreshOrLoadMoreListener.onRefresh(frame);
		}
	}

	@Override
	public void onLoadMore(LoadMoreContainer frame) {
		// 开始加载更多
		if (mOnRefreshOrLoadMoreListener != null) {
			mOnRefreshOrLoadMoreListener.onLoadMore(frame);
		}
	}

	private OnRefreshOrLoadMoreListener mOnRefreshOrLoadMoreListener;

	/**
	 * 设置下拉刷新以及加载更多的时候的回调函数
	 * 
	 * @param l
	 */
	public void setOnRefreshOrLoadMoreListener(OnRefreshOrLoadMoreListener l) {
		mOnRefreshOrLoadMoreListener = l;
	}

	public OnRefreshOrLoadMoreListener getOnRefreshOrLoadMoreListener() {
		return mOnRefreshOrLoadMoreListener;
	}

	public void setOnItemClickListener(OnItemClickListener listener) {
		mListView.setOnItemClickListener(listener);
	}

	/**
	 * 下拉刷新跟加载更多的时候调用的
	 * 
	 * @Description: TODO()
	 * @author wangzhiqiang
	 * @date 2016-3-10 上午9:47:57
	 * 
	 */
	public interface OnRefreshOrLoadMoreListener {
		/**
		 * 刷新开始
		 * 
		 * @param frame
		 *            下拉刷新控件
		 */
		void onRefresh(PtrFrameLayout frame);

		/**
		 * 加载更多的时候
		 * 
		 * @param frame
		 */
		void onLoadMore(LoadMoreContainer frame);
	}

	/**
	 * 加载更多异常
	 * 
	 * @param emptyResult
	 * @param hasMore
	 */
	public void handlerLoadMoreFinish(boolean emptyResult, boolean hasMore) {
		mLoadMoreLayout.loadMoreFinish(emptyResult, hasMore);
	}

	/**
	 * 加载更多出现错误
	 * 
	 * @param errorCode
	 * @param errorMessage
	 */
	public void handlerLoadMoreError(int errorCode, String errorMessage) {
		mLoadMoreLayout.loadMoreError(errorCode, errorMessage);
	}

	public void setAdapter(ListAdapter adapter) {
		mListView.setAdapter(adapter);
	}

	public ListAdapter getAdapter() {
		return mListView.getAdapter();
	}

	/*** ------------------emptyView-------------------------- **/
	/**
	 * 隐藏
	 */
	public void hide() {
		if (empty_loading != null) {
			empty_loading.hide();

		}
	}

	/**
	 * 空
	 */
	public void empty() {
		if (empty_loading != null) {
			empty_loading.empty();
		}
	}

	/**
	 * 加载
	 */
	public void loading() {
		if (empty_loading != null) {
			empty_loading.loading();
		}
	}

	/**
	 * 刷新 list 状态
	 * 
	 * @param isEmpty
	 *            是否是空
	 * @param isError
	 *            是否是出错了
	 * @param isFromRefresh
	 *            是否是刷新加载
	 * @param message
	 *            错误提示
	 * @return
	 */
	public boolean refreshLoadMoreUI(boolean isEmpty, boolean isError,
			boolean isFromRefresh, String message) {
		boolean shouldShowClearData = false;
		refreshComplete();
		if (isFromRefresh && (!isError) && isEmpty) {// 暂无数据
			shouldShowClearData = true;
			empty();
			handlerLoadMoreFinish(isEmpty, !isEmpty);
			if (!TextUtils.isEmpty(message)) {
				Toast.makeText(getContext(), message, 0).show();
			}
		} else if (isFromRefresh && (!isError) && (!isEmpty)) {// 成功显示
			shouldShowClearData = true;
			hide();
			handlerLoadMoreFinish(isEmpty, !isEmpty);
		} else if (isFromRefresh && isError) {// 第一页加载失败
			shouldShowClearData = true;
			empty();
			handlerLoadMoreFinish(isEmpty, !isEmpty);
			if (!TextUtils.isEmpty(message)) {
				Toast.makeText(getContext(), message, 0).show();
			}
		} else if ((!isFromRefresh) && (!isError) && (!isEmpty)) {// 多页的时候加载成功
			shouldShowClearData = false;
			hide();
			handlerLoadMoreFinish(isEmpty, !isEmpty);
		} else if ((!isFromRefresh) && (!isError) && isEmpty) {// 多页 夹在成功 数据为空
			shouldShowClearData = false;
			hide();
			handlerLoadMoreFinish(false, false);
		} else if ((!isFromRefresh) && isError) {// 多页 加载数据失败
			shouldShowClearData = false;
			hide();
			handlerLoadMoreFinish(isEmpty, !isEmpty);
			if (!TextUtils.isEmpty(message)) {
				Toast.makeText(getContext(), message, 0).show();
			}
		}
		return shouldShowClearData;
	}
}
